package com.ssm.websocket.config;

import com.ssm.websocket.entrance.ChatWebSocketHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

/**
 * https://www.cnblogs.com/kiwifly/p/11729304.html
 */
@EnableWebSocket
@Configuration(proxyBeanMethods = false)
public class WebSocketConfig implements WebSocketConfigurer {
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // https://www.ruanyifeng.com/blog/2017/05/websocket.html
        // https://blog.csdn.net/f641385712/article/details/127490724
        // https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#websocket
        // org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter
        registry.addHandler(new ChatWebSocketHandler(), "chat").setAllowedOriginPatterns("*");
    }

    // @Bean
    ServerEndpointExporter serverEndpoint() {
        return new ServerEndpointExporter();
    }

    // @Bean
    ServletServerContainerFactoryBean webSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxTextMessageBufferSize(8192);
        container.setMaxBinaryMessageBufferSize(8192);
        return container;
    }
}